import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import Login from "../views/login/login.vue";
import Main from "../views/main/main.vue";
import localCache from '@/utils/cache'

import { firstMenu } from '@/utils/map-menus'

const routes: Array<RouteRecordRaw> = [
  {
    path:"/",
    redirect:"/main"
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/main",
    name: "main",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/main/main.vue"),
  },
  {
    path:'/:pathMatch(.*)*',
    name:'notFound',
    component:() => import('@/views/not-found/not-found.vue')
  }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

router.beforeEach((to) => {
  if(to.path !== '/login'){
    const token = localCache.getCache('token')
    if(!token) {
      return '/login'
    }
  }
  if(to.path === '/main'){
    return firstMenu.url
  }
})

export default router;
